package com.tyf.common.web.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.tyf.common.web.bind.method.annotation.CurrentUserMethodArgumentResolver;
import com.tyf.common.web.bind.method.annotation.PageableMethodArgumentResolver;
import com.tyf.common.web.bind.method.annotation.SearchableMethodArgumentResolver;
import com.tyf.common.web.controller.advice.DefaultExceptionHandler;
import com.tyf.common.web.fastjson.EudFastJsonHttpMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.ResourceUtils;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liutao<br>
 * @ClassName: CommonWebConfiguration<br>
 * @Description: common-web 工程 Bean 相关配置<br>
 * @date 2016年9月28日上午9:48:21<br>
 */
@Configuration
public class CommonWebConfiguration extends WebMvcConfigurerAdapter {
    public static final Charset UTF8_CHARSET = Charset.forName("UTF-8");


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //这里一定要加classpath，不然不能找到resources下的路径
        registry.addResourceHandler("/templates/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/templates/");
        registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/static/");

        super.addResourceHandlers(registry);

    }

    /**
     * 添加自定义的参数解析器
     */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        super.addArgumentResolvers(argumentResolvers);
        argumentResolvers.add(getCurrentUserResolver());
        argumentResolvers.add(getPageableResolver());
        argumentResolvers.add(getSearchableResolver());
    }

    /**
     * 使用fastjson作为httpMessage转换器
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);

        //增加String Http Message Converter编码
        List<MediaType> mediaTypeList = new ArrayList<>();
        mediaTypeList.add(MediaType.APPLICATION_JSON_UTF8);
        mediaTypeList.add(MediaType.valueOf("text/html;charset=UTF-8"));
        mediaTypeList.add(MediaType.valueOf("text/plain;charset=UTF-8"));

        //
        EudFastJsonHttpMessageConverter fastConverter = new EudFastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        fastConverter.setFastJsonConfig(fastJsonConfig);
        fastConverter.setSupportedMediaTypes(mediaTypeList);
        converters.add(fastConverter);
    }


    /**
     * 注册默认的异常处理bean，为ControllerAdvice
     *
     * @return
     */
    @Bean
    public DefaultExceptionHandler registerDefaultExceptionHandler() {
        return new DefaultExceptionHandler();
    }

    /**
     * 获取@CurrentUser的方法参数解析器
     *
     * @return
     */
    @Bean
    public CurrentUserMethodArgumentResolver getCurrentUserResolver() {
        return new CurrentUserMethodArgumentResolver();
    }

    /**
     * 获取Pageable的方法参数解析器
     *
     * @return
     */
    @Bean
    public PageableMethodArgumentResolver getPageableResolver() {
        return new PageableMethodArgumentResolver();
    }

    /**
     * 获取Pageable的方法参数解析器
     *
     * @return
     */
    @Bean
    public SearchableMethodArgumentResolver getSearchableResolver() {
        return new SearchableMethodArgumentResolver();
    }


}
